package cn.leetcode;

public class Code543 {

    private int i = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        if(root == null){
            return 0;
        }
        int length0 = length(root.left);
        int length1 = length(root.right);
        int j = length1 + length0;
        return Math.max(i, j);
    }

    public int length(TreeNode root){
        if(root == null){
            return 0;
        }
        int length1 = length(root.left);
        int length2 = length(root.right);
        int ii = length1 + length2;
        if(ii > i){
            i = ii;
        }
        return Math.max(length1, length2) + 1;
    }
}
